﻿using CloudMusic.Helper;
using CloudMusic.Model;
using CloudMusic.ViewModel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CloudMusic.View
{
    /// <summary>
    /// PageDaySongs.xaml 的交互逻辑
    /// </summary>
    public partial class PageDaySongs : Page
    {
        public PageDaySongs()
        {
            InitializeComponent();
            this.DataContext =DataContext as PageDaySongsViewModel;
        }
        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            MusicViewModelBase.songListEvent += MusicViewModelBase_songListEvent;
            MusicViewModelBase.likesState += MusicViewModelBase_likesState;
        }
        /// <summary>
        /// 对每日歌曲喜欢状态的改变
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MusicViewModelBase_likesState(object sender, EventArgs e)
        {
            long id = (long)sender;
            var item = (ObservableCollection<MusicModel>)DaySongsList.ItemsSource;
            if (item.Any(t => t.Id == id))
            {
                var index = item.IndexOf(item.FirstOrDefault(t => t.Id == id));
                if (item[index].Like == 0)
                {
                    item[index].Like = 1;
                }
                else
                {
                    item[index].Like = 0;
                }

            }
        }
        /// <summary>
        /// 每日歌曲内容时间处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MusicViewModelBase_songListEvent(object sender, EventArgs e)
        {
            long id = (long)sender;
            if ((ObservableCollection<MusicModel>)DaySongsList.ItemsSource != null)
            {
                var item = ObjectToEntityHelper.ConvertObjectByJson<PlayModel>((ObservableCollection<MusicModel>)DaySongsList.ItemsSource);
                if (!PlayListHelper.MainMusicList.Any(t => t.Id == id))
                {
                    item.ForEach(p => PlayListHelper.MainMusicList.Add(p));
                }
            }

        }
        /// <summary>
        /// 滑动条
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DaySongsList_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
        {
            CommonHelper.RunScorllView(sender, e);
        }
        /// <summary>
        /// 加入播放列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DaySongsList_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            MusicModel music = (MusicModel)DaySongsList.SelectedValue;
            var item = ObjectToEntityHelper.ConvertObjectByJson<PlayModel>((ObservableCollection<MusicModel>)DaySongsList.ItemsSource);
            if (!PlayListHelper.MainMusicList.Any(t => t.Id == music.Id))
            {
                PlayListHelper.MainMusicList.Clear();
                item.ForEach(p => PlayListHelper.MainMusicList.Add(p));
            }
        }

        private void SingerDetail_GotFocus(object sender, MouseButtonEventArgs e)
        {
            MusicModel music = (MusicModel)DaySongsList.SelectedValue;
            long ablumId = music.SingerId;
            var ps = new PageSingerInfoViewModel(ablumId);
            PageSingerInfo pageSingerInfo = new PageSingerInfo(ps);
            NavigationService.Navigate(pageSingerInfo);
        }
        //public SolidColorBrush ListBoxBgColor { get => index % 2 == 0 ? new SolidColorBrush(Color.FromRgb(45, 47, 51)) : new SolidColorBrush(Color.FromRgb(48, 50, 54)); }
    }
}
